package com.example.a001.part11;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.a001.R;

import java.util.ArrayList;
import java.util.List;

public class ItemActivity extends AppCompatActivity {

    private ListView chatListView;
    private EditText messageInput;
    private Button sendButton;
    private ArrayAdapter<String> adapter;
    private List<String> chatMessages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_item);

        chatListView = findViewById(R.id.chat_list);
        messageInput = findViewById(R.id.message_input);
        sendButton = findViewById(R.id.send_button);

        chatMessages = new ArrayList<>();
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, chatMessages);
        chatListView.setAdapter(adapter);

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = messageInput.getText().toString().trim();
                if (!message.isEmpty()) {
                    chatMessages.add("You: " + message);
                    messageInput.setText("");
                    String response = getResponse(message);
                    chatMessages.add("Bot: " + response);
                    adapter.notifyDataSetChanged();
                    chatListView.setSelection(chatMessages.size() - 1);
                }
            }
        });

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    private String getResponse(String message) {
        // 简单的响应逻辑
        if (message.toLowerCase().contains("hello")) {
            return "Hello!";
        } else if (message.toLowerCase().contains("help")) {
            return "How can I help you today?";
        }
        else if (message.toLowerCase().contains("你好")) {
            return "你好!";
        }
        else if (message.toLowerCase().contains("bye")) {
            return "Goodbye!";
        } else {
            return "I'm not sure how to respond to that.";
        }
    }
}
